package com.allwees.bs.c.module.user.controller;

import com.allwees.bs.c.module.base.controller.BaseController;
import com.allwees.bs.c.module.user.dto.ShippingAddressDTO;
import com.allwees.bs.c.module.user.dto.UserDto;
import com.allwees.bs.c.module.user.req.RegionReq;
import com.allwees.bs.c.module.user.req.ShippingAddressReq;
import com.allwees.bs.c.module.user.req.ShippingAddressUpdateDefaultReq;
import com.allwees.bs.c.module.user.req.VisitorShippingAddressReq;
import com.allwees.bs.c.module.user.service.IShippingAddressService;
import com.allwees.bs.core.modelbase.page.PaginateDTO;
import com.allwees.bs.core.modelbase.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 和个人相关的信息
 * 收货地址相关
 *
 * @author jy2016
 */
@RestController
@RequestMapping("/api/v1/user")
@Validated
public class ShippingAddressController extends BaseController {

    @Autowired
    private IShippingAddressService shippingAddressService;


    /**
     * 添加收货地址
     *
     * @param req 收货地址信息
     * @return 创建后的收货地址信息, 前端使用ID在后续的接口中使用
     * @see ShippingAddressReq 需要传递的参数
     */
    @PostMapping("/shippingAddress")
    public R addShippingAddress(@RequestBody @Validated ShippingAddressReq req) {
        ShippingAddressDTO shippingAddressDTO = shippingAddressService.addShippingAddress(getUserUuid(), req);
        return R.ok(shippingAddressDTO);
    }

    /**
     * 添加收货地址
     *
     * @param req 收货地址信息
     * @return 创建后的收货地址信息, 前端使用ID在后续的接口中使用
     * @see ShippingAddressReq 需要传递的参数
     */
    @PostMapping("/visitorShippingAddress")
    public R visitorAddShippingAddress(@RequestBody @Validated VisitorShippingAddressReq req) {
        req.validateSignature();

        UserDto result = shippingAddressService.visitorAddShippingAddress(req);
        return R.ok(result);
    }

    /**
     * 获取自己的所有收货地址(标准翻页)
     *
     * @return 列表
     */
    @GetMapping("/shippingAddress")
    public R getAllShippingAddress() {
        PaginateDTO<ShippingAddressDTO> page = shippingAddressService.getAllShippingAddress(getUserUuid());
        return R.ok(page);
    }

    /**
     * 获取自己的默认收货地址
     *
     * @return 收货地址
     */
    @GetMapping("/shippingAddress/default")
    public R getDefaultShippingAddress() {
        ShippingAddressDTO address = shippingAddressService.getDefaultShippingAddress(getUserUuid());
        return R.ok(address);
    }

    /**
     * 获取自己的指定ID的收货地址
     *
     * @param addressUuid 收货地址iD
     * @return 收货地址
     */
    @GetMapping("/shippingAddress/{addressUuid}")
    public R getShippingAddress(@PathVariable("addressUuid") String addressUuid) {
        return R.ok(shippingAddressService.getShippingAddress(getUserUuid(), addressUuid));
    }

    /**
     * 获取自己的指定ID的收货地址
     *
     * @param addressUuid
     *            收货地址iD
     * @return 收货地址
     */
    /**
     * 按照regionId根据获取下级region
     *
     * @param regionReq
     * @return
     */
    @PostMapping("/region/query")
    public R getRegionList(@RequestBody RegionReq regionReq) {
        return R.ok(shippingAddressService.findSubRegionList(regionReq));
    }

    /**
     * 修改收货地址
     *
     * @param addressUuid 收货地址ID
     * @return 收货地址
     */
    @PutMapping("/shippingAddress/{addressUuid}/set-default")
    public R updateShippingAddress(@PathVariable("addressUuid") String addressUuid, @RequestBody ShippingAddressUpdateDefaultReq req) {
        shippingAddressService.updateDefault(getUserUuid(), addressUuid, req);
        return R.ok();
    }

    /**
     * 更新指定id的收货地址
     *
     * @param addressUuid       收货地址ID
     * @param shippingAddressVo 修改后的收货地址信息
     * @return 收货地址
     */
    @PutMapping("/shippingAddress/{addressUuid}")
    public R updateShippingAddress(@PathVariable("addressUuid") String addressUuid,
                                   @RequestBody @Validated ShippingAddressReq shippingAddressVo) {
        return R.ok(shippingAddressService.updateShippingAddress(getUserUuid(), addressUuid, shippingAddressVo));
    }

    /**
     * 删除指定id的收货地址
     *
     * @param addressUuid 收货地址ID
     * @return 成功标志，无业务数据
     */
    @DeleteMapping("/shippingAddress/{addressUuid}")
    public R getAllShippingAddress(@PathVariable("addressUuid") String addressUuid) {
        // 删除成功不需要返回内容
        shippingAddressService.deleteShippingAddress(getUserUuid(), addressUuid);
        return R.ok();
    }
}
